package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;
import java.util.List;
import java.util.Random;

public class Arrow extends AbstractMapleObject{
    public static Image[] imgs = {
            ImageUtil.getImage("hero_right_arrow"),
            ImageUtil.getImage("hero_left_arrow"),

    };
    public boolean live = true;
    public Image img;
    public Arrow(){
        this.speed =20;
        this.width = imgs[0].getWidth(null);
        this.height = imgs[0].getHeight(null);
    }

    public Arrow(MapleStoryClient msc, int x, int y, Direction dir){
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.dir = dir;
        /*if (dir == Direction.RIGHT){
            this.img = imgs[0];
        }else{
            this.img = imgs[1];
        }*/
     }
    @Override
    public void draw(Graphics g) {
        if (!live){
            msc.arrows.remove(this);
            return;
        }
        switch (dir){
            case RIGHT:
                g.drawImage(imgs[0],x,y,null);
                break;
            case LEFT:
                g.drawImage(imgs[1],x,y,null);
                break;
            default:
                break;
        }
        move();
    }

    @Override
    public void move() {
        switch (dir){
            case RIGHT:
                this.x += speed;
                break;
            case LEFT:
                this.x -= speed;
                break;
            default:
                break;
        }
        outOfBound();

    }

    /**
     * 弓箭出界判断
     */
    private void outOfBound(){
        if (this.x < -500|| this.x > Constant.GAME_WIDTH +500){
            this.live = false;
        }
    }

    /**
     * 随即道具生成器
     */
    public static Random r = new Random();

    /**
     * 弓箭射中小怪的方法
     * @param mob 小怪对象
     * @return 是否碰撞
     */
    private boolean hit(Mob mob){
         if (this.live && this.getRectangle().intersects(mob.getRectangle()) && mob.action != Action.DIE){
             this.live = false;
            // msc.hero.hitValue = r.nextInt(msc.hero.ATT - 5) + 8;//8~12

             msc.hero.hitValue = (int) (msc.hero.ATT * (1 + r.nextDouble()));
             //创建出伤害值
             Power power = new Power(msc,mob.x,mob.y);
             msc.powers.add(power);
             mob.HP -= power.value;
             if (mob.HP <=0){
                 mob.action = Action.DIE;
                 if (r.nextInt(100)<100){
                     Item item = new Item(msc,mob.x + mob.width / 2 - 14,mob.y+mob.height -30,r.nextInt(4));
                     msc.items.add(item);
                 }

                 if (msc.hero.exp <400){
                     msc.hero.exp +=100;

                 }else{
                     msc.hero.exp = 0;
                     msc.hero.grade +=1;
                     msc.hero.HP =  msc.hero.MAX_HP;
                     msc.hero.MP =  msc.hero.MAX_MP;
                 }
             }

              //mob.live = false;
             return true;
         }
        return false;
    }

    /**
     * 一直弓箭与所有怪进行碰撞检测的方法
     * @param mobs 怪物容器
     * @return 是否击中
     */
    public boolean hit(List<Mob> mobs){
        for (int i = 0; i < mobs.size(); i++) {
            Mob mob = mobs.get(i);
            if (hit(mob)){
                return true;
            }
        }
        return false;
    }
}
